package org.springsecuritylearn.admin.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author hyf
 * @Title: ${file_name}
 * @Package org.springsecuritylearn.admin.interceptor
 * @Description: ${todo}
 * @date 2018/5/6上午9:35
 */
@Component
public class TimeInterceptor  implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(((HandlerMethod)handler).getBean().getClass().getName());
        System.out.println(((HandlerMethod)handler).getMethod().getName());
        request.setAttribute("startTIme",System.currentTimeMillis());
        return  true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("服务处理耗时："+(System.currentTimeMillis()-(long)request.getAttribute("startTIme")));

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion服务处理耗时："+(System.currentTimeMillis()-(long)request.getAttribute("startTIme")));
        System.out.println("ex:"+ex.getStackTrace());
    }
}
